/*
    Copyright (C) 2008 Edwin Lee

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var API_CALL_URL = "http://api.flickr.com/services/rest/";
var API_CALL_METHOD_KEY = "method";
var API_CALL_APIKEY_KEY = "api_key";
var API_CALL_USERNAME_KEY = "username";
var API_CALL_NSID_KEY = "user_id";
var API_CALL_PERPAGE_KEY = "per_page";
var API_CALL_PAGE_KEY = "page";

var METHODNAME_PEOPLE_FINDBYUSERNAME = "flickr.people.findByUsername";
var METHODNAME_PEOPLE_GETPUBLICPHOTOS = "flickr.people.getPublicPhotos";

var RESPONSE_ELEMENT_RSP = "rsp";
var RESPONSE_ELEMENT_USER = "user";
var RESPONSE_ELEMENT_PHOTOS = "photos";
var RESPONSE_ELEMENT_PHOTO = "photo";

var RESPONSE_ATTRIBUTE_NSID = "nsid";
var RESPONSE_ATTRIBUTE_PAGES = "pages";
var RESPONSE_ATTRIBUTE_ID = "id";
var RESPONSE_ATTRIBUTE_SECRET = "secret";
var RESPONSE_ATTRIBUTE_SERVER = "server";
var RESPONSE_ATTRIBUTE_FARM = "farm";
var RESPONSE_ATTRIBUTE_TITLE = "title";

var GETPHOTOS_PERPAGE_MAX = 500;

User = function(username, nsid)
{
	var user = new Object();
	
	user.username = username;
	user.nsid = nsid;
	
	return user;
}

Photo = function(id, secret, server, farm, title)
{
	var photo = new Object();
	
	photo.id = id;
	photo.secret = secret;
	photo.server = server;
	photo.farm = farm;
	photo.title = title;
	
	return photo;
}

callFlickrMethod = function(flickrMethod, apiKey, keyValuePairs, bustCache)
{
	if (!keyValuePairs)
	{
		keyValuePairs = new Array();
	}
	
	keyValuePairs[keyValuePairs.length] = KeyValuePair(API_CALL_METHOD_KEY, flickrMethod);
	keyValuePairs[keyValuePairs.length] = KeyValuePair(API_CALL_APIKEY_KEY, apiKey);
	
	return (httpGetWithParams(API_CALL_URL, keyValuePairs, bustCache));
}

getUserNsid = function(apiKey, username)
{
	var keyValuePairs = new Array();
	
	keyValuePairs[keyValuePairs.length] = KeyValuePair(API_CALL_USERNAME_KEY, username);
	
	var responseXml = callFlickrMethod(METHODNAME_PEOPLE_FINDBYUSERNAME, apiKey, keyValuePairs, false)[HTTPGET_RSPTYPE_XML_IDX];
	
	if (responseXml)
	{
		var rsp = responseXml.getElementsByTagName(RESPONSE_ELEMENT_RSP)[0];
		
		var user = rsp.getElementsByTagName(RESPONSE_ELEMENT_USER)[0];
		
		var nsid = user.getAttribute(RESPONSE_ATTRIBUTE_NSID);
		
		return nsid;
	}
	
	return null;
}

getUserByUsername = function(apiKey, username)
{
	var nsid = getUserNsid(apiKey, username);
	
	if (nsid)
	{
		return (User(username, nsid));
	}
	
	return null;
}

getUserPhotos = function(apiKey, nsid, numPhotos)
{
	var perPage = ((numPhotos != -1) && (numPhotos <= GETPHOTOS_PERPAGE_MAX)) ? numPhotos : GETPHOTOS_PERPAGE_MAX;
	var page = 1;
	var getMore = true;
	
	var userPhotos = new Array();
	
	while (getMore)
	{
		var keyValuePairs = new Array();
		
		keyValuePairs[keyValuePairs.length] = KeyValuePair(API_CALL_NSID_KEY, nsid);
		keyValuePairs[keyValuePairs.length] = KeyValuePair(API_CALL_PERPAGE_KEY, perPage);
		keyValuePairs[keyValuePairs.length] = KeyValuePair(API_CALL_PAGE_KEY, page);
		
		var responseXml = callFlickrMethod(METHODNAME_PEOPLE_GETPUBLICPHOTOS, apiKey, keyValuePairs, false)[HTTPGET_RSPTYPE_XML_IDX];
		
		if (responseXml)
		{
			var rsp = responseXml.getElementsByTagName(RESPONSE_ELEMENT_RSP)[0];
			
			var photos = rsp.getElementsByTagName(RESPONSE_ELEMENT_PHOTOS)[0];
			
			var pages = parseInt(photos.getAttribute(RESPONSE_ATTRIBUTE_PAGES), 10);
			
			var photoNodes = photos.getElementsByTagName(RESPONSE_ELEMENT_PHOTO);
			
			for (var i = 0; ((i < photoNodes.length) && ((numPhotos == -1) || (userPhotos.length < numPhotos))); i++)
			{
				var photo = photoNodes[i];
				
				var id = photo.getAttribute(RESPONSE_ATTRIBUTE_ID);
				var secret = photo.getAttribute(RESPONSE_ATTRIBUTE_SECRET);
				var server = photo.getAttribute(RESPONSE_ATTRIBUTE_SERVER);
				var farm = photo.getAttribute(RESPONSE_ATTRIBUTE_FARM);
				var title = photo.getAttribute(RESPONSE_ATTRIBUTE_TITLE);
				
				userPhotos[userPhotos.length] = Photo(id, secret, server, farm, title);
			}
			
			getMore = (page < pages) && ((numPhotos == -1) || (userPhotos.length < numPhotos));
			
			page++;
		}
		else
		{
			getMore = false;
		}
	}
	
	return userPhotos;
}

getPhotoStream = function(farm, server, id, secret, size)
{
	var imageUrl = "http://farm" + farm + ".static.flickr.com/" + server + "/" + id + "_" + secret;
	
	if ((size == VALUE_SIZE_THUMBNAIL) || (size == VALUE_SIZE_SMALL))
	{
		imageUrl += ("_" + size);
	}
	
	imageUrl += ".jpg";
	
	return (httpGet(imageUrl, false)[HTTPGET_RSPTYPE_STREAM_IDX]);
}

getPhotoPageUrl = function(nsid, id)
{
	var photoPageUrl = "http://www.flickr.com/photos/" + nsid + "/" + id + "/";
	
	return photoPageUrl;
}
